<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 借书表逻辑
 */
class Borrow extends AdminBase
{
    
    /**
     * 获取借书表列表
     */
    public function getBorrowList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        $this->modelBorrow->alias('b');
        $join=[
            ['ob_book k','k.b_id=b.b_id'],
        ];
        $field='b.*,k.name as name,k.in_number as in_number,k.out_number as out_number';
        $this->modelBorrow->join = $join;
        return $this->modelBorrow->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 借书表信息编辑
     */
    public function borrowEdit($data = [])

    {

        $where=['b_id'=>$data['b_id']];

        $validate_result = $this->validateBorrow->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateBorrow->getError()];
        }
        $num1 = "$data[in_number]"-1;
        $num2 = "$data[out_number]"+1;
        $url = url('borrowList');

        $borrowData = [
            'b_id' => $data['b_id'],
            's_id' => $data['s_id'],
            'm_id' => $data['m_id'],
            'should_back_time' => $data['should_back_time'],
        ];
        
        $result = $this->modelBorrow->setInfo($borrowData);

        $result1 = $this->modelBook->setFieldValue(['b_id' => $data['b_id']], 'in_number',$num1);
        $result2 = $this->modelBook->setFieldValue(['b_id' => $data['b_id']], 'out_number',$num2);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '借书表' . $handle_text . '，name：' . $data['s_id']);
        

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelBorrow->getError()];
    }

    /**
     * 还书信息编辑
     */
    public function backEdit($data = [])

    {

        $where=['b_id'=>$data['b_id']];
        $num1 = "$data[in_number]"+1;
        $num2 = "$data[out_number]"-1;
        $url = url('borrowList');

        $result1 = $this->modelBook->setFieldValue(['b_id' => $data['b_id']], 'in_number',$num1);
        $result2 = $this->modelBook->setFieldValue(['b_id' => $data['b_id']], 'out_number',$num2);
        
        $result1 ;
        

        return $result1 ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelBorrow->getError()];
    }


    /**
     * 获取借书表信息
     */
    public function getBorrowInfo($where = [], $field = true)
    {
        return $this->modelBorrow->getInfo($where, $field);
    }
    
    /**
     * 借书表删除
     */
    public function borrowDel($where = [])
    {
        
        $result = $this->modelBorrow->deleteInfo($where);
        
        $result && action_log('删除', '借书表删除' . '，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelBorrow->getError()];
    }
}
